import UIKit
@available(iOSApplicationExtension, unavailable)
internal extension IQKeyboardManager {
    func responderViews() -> [UIView]? {
        var superConsideredView: UIView?
        for disabledClass in toolbarPreviousNextAllowedClasses {
            superConsideredView = textFieldView?.superviewOfClassType(disabledClass)
            if superConsideredView != nil {
                break
            }
        }
        if let view = superConsideredView {
            return view.deepResponderViews()
        } else { 
            guard let textFields = textFieldView?.responderSiblings() else {
                return nil
            }
            switch toolbarManageBehaviour {
            case .bySubviews:   return textFields
            case .byTag:    return textFields.sortedArrayByTag()
            case .byPosition:    return textFields.sortedArrayByPosition()
            }
        }
    }
    func privateIsEnabled() -> Bool {
        var isEnabled = enable
        let enableMode = textFieldView?.enableMode
        if enableMode == .enabled {
            isEnabled = true
        } else if enableMode == .disabled {
            isEnabled = false
        } else if var textFieldViewController = textFieldView?.viewContainingController() {
            if textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
                textFieldViewController = topController
            }
            if !isEnabled, enabledDistanceHandlingClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
                isEnabled = true
            }
            if isEnabled {
                if disabledDistanceHandlingClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
                    isEnabled = false
                }
                if isEnabled {
                    let classNameString: String = "\(type(of: textFieldViewController.self))"
                    if classNameString.contains("UIAlertController"), classNameString.hasSuffix("TextFieldViewController") {
                        isEnabled = false
                    }
                }
            }
        }
        return isEnabled
    }
    func privateIsEnableAutoToolbar() -> Bool {
        guard var textFieldViewController = textFieldView?.viewContainingController() else {
            return enableAutoToolbar
        }
        if textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
            textFieldViewController = topController
        }
        var enableToolbar = enableAutoToolbar
        if !enableToolbar, enabledToolbarClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
            enableToolbar = true
        }
        if enableToolbar {
            if disabledToolbarClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
                enableToolbar = false
            }
            if enableToolbar {
                let classNameString: String = "\(type(of: textFieldViewController.self))"
                if classNameString.contains("UIAlertController"), classNameString.hasSuffix("TextFieldViewController") {
                    enableToolbar = false
                }
            }
        }
        return enableToolbar
    }
    func privateShouldResignOnTouchOutside() -> Bool {
        var shouldResign = shouldResignOnTouchOutside
        let enableMode = textFieldView?.shouldResignOnTouchOutsideMode
        if enableMode == .enabled {
            shouldResign = true
        } else if enableMode == .disabled {
            shouldResign = false
        } else if var textFieldViewController = textFieldView?.viewContainingController() {
            if textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
                textFieldViewController = topController
            }
            if !shouldResign, enabledTouchResignedClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
                shouldResign = true
            }
            if shouldResign {
                if disabledTouchResignedClasses.contains(where: { textFieldViewController.isKind(of: $0) }) {
                    shouldResign = false
                }
                if shouldResign {
                    let classNameString: String = "\(type(of: textFieldViewController.self))"
                    if classNameString.contains("UIAlertController"), classNameString.hasSuffix("TextFieldViewController") {
                        shouldResign = false
                    }
                }
            }
        }
        return shouldResign
    }
}
